<template>
  <div>
  </div>
</template>
<script type="text/ecmascript-6">
  import {setStore, getStore} from '../../../test/unit/storage'
  import {getAuthorUrl, getOpenInfo, userIsExist} from '../../../test/unit/http'
  import {Toast} from 'we-vue'

  export default {
    name: 'author',
    props: {},
    data() {
      return {};
    },
    methods: {
      goAuthorUrl(params) {
        getAuthorUrl(params).then(res => {
          if (res.code == 100) {
            console.log(res.object.url);
            window.location.href = res.object.url;
          } else {
            Toast.fail('获取微信授权地址失败');
          }
        })
      },
      getOpenInfo(code) {
        getOpenInfo({code: code}).then(res => {
          console.log(res);
          if (res.code == 100) {
            let openId = res.object.openId;
            if (openId) {
              setStore("openId", openId);
              this.userIsExist({openId: openId})
            } else {
              setTimeout(function () {
                this.$router.push({path: '/'})
              }, 2 * 1000);
            }
          } else {
            Toast.fail('获取用户信息失败');
          }
        })
      },
      userIsExist(params) {
        userIsExist(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            let isExist = res.object.isExist;
            let backUrl = this.$myUtils.GetUrlParam("backUrl");
            let baseUrl = this.$myUtils.getBaseUrl(backUrl);
            let url = this.$myUtils.ridUrlParam(window.location.href, ['code', 'state', 'backUrl']);
            let backUrlParams = this.$myUtils.getAllUrlParam(url);
            if (isExist) {
              let token = res.object.token;
              setStore('token', token);
              if (backUrl) {
                let turnUrl = baseUrl + '#/' + backUrl + '?' + backUrlParams;
                console.info('turnUrl', turnUrl);
                window.location.href = turnUrl;
              } else {
                let isHave = res.object.isHave;
                if (isHave) {
                  window.location.href = baseUrl + "#/index";
                } else {
                  window.location.href = baseUrl + "#/cata";
                }
              }
            } else {
              if (backUrl) {
                window.location.href = baseUrl + "#/register?backUrl=" + backUrl + '&' + backUrlParams;
              } else {
                window.location.href = baseUrl + "#/register";
              }
            }
          }
        })
      }
    },
    created() {
      let openId = getStore("openId");
      if (openId) {
        this.userIsExist({openId: openId})
      } else {
        let myUtils = this.$myUtils;
        let code = myUtils.GetUrlParam('code'); // 截取code
        if (myUtils.isNull(code)) {
          let router = window.location.href.split("#")[0];
          let params = {router: router};
          this.goAuthorUrl(params);
        } else {
          this.getOpenInfo(code);
        }
      }
    },
    components: {}
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
  .banner img {
    object-fit: cover;
  }

  .weui-btn_primary {
    background-color: #2e94f0;
    margin-top: 30px;
  }

  button.weui-btn {
    width: 94%;
    margin-top: 30px;
  }

  .weui-btn_primary a {
    color: #fff;
  }

  .register {
    text-align: right;
    padding-right: 15px;
    margin-top: 20px;
  }

  .register a {
    color: #f37670;
  }

  .agree {
    position: absolute;
    bottom: 30px;
    left: 50%;
    margin-left: -160px;
  }

  .agree span {
    color: #2d93f0;
  }
</style>
